import React from 'react'
import { StyleSheet, View, Text, FlatList } from 'react-native'
import { SafeAreaView } from 'react-navigation'
import { connect } from 'react-redux'
import AsyncStorage from '@react-native-async-storage/async-storage';
import DeviceList from './components/DeviceList'

@connect(({ connectedDevice }) => ({
  devices: connectedDevice.list
}))
export default class Device extends React.PureComponent {
  static navigationOptions = {
    title: '已连接的设备'
  }

  getData = async () => {
    try {
      const jsonValue = await AsyncStorage.getItem('bluenames');
      console.log(jsonValue,'112233');
      return jsonValue != null ? JSON.parse(jsonValue) : null;
    } catch(e) {
      // error reading value
      console.log(e,'Device21111120210302')
      // return 'test2';
    }
  }
  
  onPressDevice = (item) => {
    const { navigation } = this.props
    navigation.push('Service', { device: item })
  }

  render() {
    const { devices } = this.props;
    return (
      <SafeAreaView style={styles.container}>
        <DeviceList data={devices} onPress={this.onPressDevice} />
      </SafeAreaView>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  }
})